package com.ebupt.migu.music.autoTri.util;

import com.ebupt.migu.music.autoTri.Entity.PersonCount;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuhongsong
 * @version 1.0
 * @date 2020/7/31 10:24
 */
@Slf4j
public class fileUtil {

    //  读取文件

    /**
     * 根据文件将获取到的内容转化为List
     *
     * @param path
     * @return List<String>
     */
    public static List<String> getFileContext(String path) {
        FileReader fileReader;
        List<String> list;
        BufferedReader bufferedReader;
        try {
            fileReader = new FileReader(path);
            bufferedReader = new BufferedReader(fileReader);
            String str = null;
            list = new ArrayList<String>();
            while ((str = bufferedReader.readLine()) != null) {
                if (str.trim().length() > 2) {
                    list.add(str);
                }
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 根据文件将获取到的内容转化为List
     *
     * @param path
     * @return List<String>
     */
    public static Map<String,Long> getFileContextofMap(String path) {
        FileReader fileReader;
        List<String> list;
        BufferedReader bufferedReader;
        try {
            fileReader = new FileReader(path);
            bufferedReader = new BufferedReader(fileReader);
            String str = null;
            Map<String,Long> map=new HashMap<>();
            while ((str = bufferedReader.readLine()) != null) {
                if (str.trim().length() > 2) {
                    map.put(str.substring(0,str.indexOf("|")),Long.parseLong(str.substring(str.lastIndexOf("|")+1)));
                }
            }
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据文件将获取到的内容转化为List
     *
     * @param path
     * @return List<String>
     */
    public static List<PersonCount> getPersonCountFile(String path) {
        FileReader fileReader;
        List<PersonCount> list=new ArrayList<>();
        BufferedReader bufferedReader;
        PersonCount personCount;
        try {
            fileReader = new FileReader(path);
            bufferedReader = new BufferedReader(fileReader);
            String str = null;
            Map<String,Long> map=new HashMap<>();
            while ((str = bufferedReader.readLine()) != null) {
                if (str.trim().length() > 2) {
                    personCount=new PersonCount(str.substring(0,str.indexOf("|")),
                            Integer.parseInt(str.substring(str.lastIndexOf("|")+1)) );
                    list.add(personCount);
                }
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> getPersonFile(String path) {
        FileReader fileReader;
        List<String> list=new ArrayList<>();
        BufferedReader bufferedReader;
        try {
            if (Boolean.FALSE.equals(isHaveFile(path))){
                return Lists.newArrayList();
            }
            fileReader = new FileReader(path);
            bufferedReader = new BufferedReader(fileReader);
            String str = null;
            Map<String,Long> map=new HashMap<>();
            while ((str = bufferedReader.readLine()) != null) {
                if (str.trim().length() > 2) {
                    list.add(str);
                }
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Boolean isHaveFile(String path){
        File file = new File(path);
        return file.exists();
    }

    public static void putFile(String fileName, List<String> contentList) {
        try {
            FileWriter fileWritter = new FileWriter(fileName,true);
            fileWritter.flush();
            String newLine = System.getProperty("line.separator");
                for (String s : contentList) {
                    fileWritter.write(s);
                    fileWritter.write(newLine);
                }
          //  randomFile.writeBytes(content);
            fileWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 清空已有的文件内容，以便下次重新写入新的内容
    public static void clearInfoForFile(String fileName) {
        File file =new File(fileName);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *   统计入文件静态类
     * @param fileName
     * @param list
     */
    public static void putFileCountList(String fileName, List<PersonCount> list) {
        try {
            // 打开一个随机访问文件流，按读写方式
            log.info("写入统计的文件路径："+fileName);
            FileWriter fileWritter = new FileWriter(fileName,true);
            fileWritter.flush();
            String newLine = System.getProperty("line.separator");
            for(PersonCount personCount :list) {
                fileWritter.write(personCount.getUid().concat("|").concat(String.valueOf(personCount.getTotal())));
                fileWritter.write(newLine);
            }
            fileWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * B方法追加文件：使用FileWriter
     */
    public static void appendMethodB(String fileName, String content) {
        try {
            //打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
